package com.sczmgk.hotline.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;

import com.sczmgk.hotline.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2016/12/29.
 */

public class TimerUtils {

    private int time;
    private Timer timer_1s;

    private final int REF_TIMER = 100;
    private final int GET_VERCODE = 101;

    private Context context;

    /**
     * 需要操作的按钮
     */
    private Button btnGetCode;


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case REF_TIMER:
                    int time = (Integer) msg.obj;
                    btnGetCode.setText(context.getString(R.string.get_vercode_timer,time+""));
                    btnGetCode.setTextColor(context.getResources().getColor(R.color.edit_hint_color));
                    break;
                case GET_VERCODE:
                    btnGetCode.setClickable(true);
                    btnGetCode.setBackgroundResource(R.drawable.get_verify_bg);
                    btnGetCode.setTextColor(context.getResources().getColor(R.color.grey_hint_color));
                    btnGetCode.invalidate();
                    btnGetCode.setText(context.getString(R.string.get_security_key));

                    break;

                default:
                    break;
            }
        }
    }  ;


    /**
     * 开启定时器60秒
     */
    @SuppressLint("ResoureAsColor")
    public void startTimer(Context context , Button btn) {
        this.context = context ;
        this.btnGetCode = btn ;

        if (timerIsRunning()){
            return ;
        }

        time = 60;
        btnGetCode.setBackgroundResource(R.drawable.get_verify_bg);
        btnGetCode.setTextColor(context.getResources().getColor(R.color.grey_hint_color));
        btnGetCode.setClickable(false);
        if (timer_1s == null) {
            timer_1s = new Timer();
        }
        timer_1s.schedule(new TimerTask() {
            @Override
            public void run() {
                time--;
                if(time < 1){
                    timer_1s.cancel();
                    Message msg1 = Message.obtain();
                    msg1.what = GET_VERCODE;
                    handler.sendMessage(msg1);

                }else{
                    Message msg = Message.obtain();
                    msg.what = REF_TIMER;
                    msg.obj = time;
                    handler.sendMessage(msg);
                }
            }
        }, 0, 1000);

    }

    public void stopTimer(Button btn){
        this.btnGetCode = btn ;

        if (timer_1s != null) {
            time = 60 ;
            timer_1s.cancel();
            Message msg = Message.obtain();
            msg.what = GET_VERCODE;
            handler.sendMessage(msg);
        }
    }

    public boolean timerIsRunning(){
        if(timer_1s != null){
            if(time < 60 && time >0){
                return true;
            }
        }
        return false;
    }

}
